package com.atguigu.controller;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController{

    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private DictService dictService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping
    //查询所有房屋信息
    public String findHouses(HttpServletRequest request,Map map){
        Map<String, Object> filters = getFilters(request);
        PageInfo<House> page = houseService.findPage(filters);
        //将数据存储到map中
        map.put("page",page);
        map.put("filters",filters);

        getDatas(map);
        //跳转到house首页
        return "house/index";
    }

    /**
     * 添加
     * @param map
     * @return
     */
    @RequestMapping("/create")
    public String create(Map map){
        getDatas(map);
        return "house/create";
    }

    /**
     * 保存
     * @param house
     * @return
     */
    @RequestMapping("/save")
    public String save(House house){
        houseService.insert(house);
        return "common/successPage";
    }


    /**
     * 根据id查找房屋信息
     * @param houseId
     * @return
     */
    @RequestMapping("/edit/{houseId}")
    public String edit(@PathVariable Long houseId,Map map){
        getDatas(map);
        House house = houseService.getById(houseId);
        map.put("house",house);
        return "house/edit";
    }

    /**
     * 修改
     * @param house
     * @return
     */
    @RequestMapping("/update")
    public String update(House house){
        houseService.update(house);
        return "common/successPage";
    }

    /**
     * 删除
     * @param houseId 要删除的id
     * @return 重定向到house页面
     */
    @RequestMapping("/delete/{houseId}")
    public String delete(@PathVariable Long houseId){
        houseService.delete(houseId);
        return "redirect:/house";
    }

    /**
     * 发布
     * @param houseId 房源id
     * @param status 状态
     * @return
     */
    @RequestMapping("/publish/{houseId}/{status}")
    public String publish(@PathVariable Long houseId,@PathVariable Integer status){
        houseService.publish(houseId,status);
        return "redirect:/house";
    }

    /**
     * 详情
     * @param houseId 房源id
     * @param map 作用域
     * @return 房源展示页面
     */
    @RequestMapping("/show/{houseId}")
    public String show(@PathVariable Long houseId,Map map){
        //1.当前房源的所有信息,放到请求域
        House house = houseService.getById(houseId);
        map.put("house",house);
        //2.当前房源所在小区的所有信息,放到请求域
        Community community = communityService.getById(house.getCommunityId());
        map.put("community",community);
        //3.当前房源图片信息.放到请求域
        List<HouseImage> houseImage1List = houseImageService.findHouseImageByHouseId(houseId, 1);
        map.put("houseImage1List",houseImage1List);
        //4.当前房产图片信息,放到请求域
        List<HouseImage> houseImage2List = houseImageService.findHouseImageByHouseId(houseId, 2);
        map.put("houseImage2List",houseImage2List);
        //5.当前房源经纪人信息,放到请求域
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerByHouseId(houseId);
        map.put("houseBrokerList",houseBrokerList);
        //5.当前房源房东信息,放到请求域
        List<HouseUser> houseUserList = houseUserService.findHouseBrokerByHouseId(houseId);
        map.put("houseUserList",houseUserList);
        return "house/show";
    }


    //将相同的数据提取
    public void getDatas(Map map){
        //查询所有小区
        List<Community> communityList = communityService.findAll();
        //查询数据字典中所有数据
//        List<Dict> communityList1 = dictService.findDictByParentCode("communityList");
        List<Dict> houseTypeList = dictService.findDictByParentCode("houseType");
        List<Dict> floorList = dictService.findDictByParentCode("floor");
        List<Dict> buildStructureList = dictService.findDictByParentCode("buildStructure");
        List<Dict> decorationList = dictService.findDictByParentCode("decoration");
        List<Dict> directionList = dictService.findDictByParentCode("direction");
        List<Dict> houseUseList = dictService.findDictByParentCode("houseUse");
        //将数据存储到map中
        map.put("communityList",communityList);
        map.put("houseTypeList", houseTypeList);
        map.put("floorList", floorList);
        map.put("buildStructureList", buildStructureList);
        map.put("decorationList",decorationList);
        map.put("directionList", directionList);
        map.put("houseUseList", houseUseList);
    }
}
